/**
 * new Promise(executor)
 * * new一个实例,意味着构造函数是类
 * * 需要一个构造器 executor
 */

class MyPromise {
  constructor(executor)
}

/**
 * Promise的三种状态
 * * pending ----> state 
 * * fullFulled ----> result
 * * rejected ----> err
 */

class MyPromise {
  constructor(executor) {
    this.state = 'pending',
      this.value = '',
      this.reason = ''
  }
}

/**
 * new Promise((resolve, reject)
 * * 构造器需要两个参数
 * * resolve
 * * reject
 */
class MyPromise {
  constructor(executor) {
    this.state = 'pending',
      this.value = '',
      this.reason = '',
      executor(resolve, reject)
  }
}


/**
 * 执行过程为resolve(result) or reject(err)
 * * 意味着resolve和reject是个函数
 * * resolve 需要把结果丢出去 --->需要接收一个参数
 * * reject也需要丢出错误 ---》需要接收一个参数
 * * 一旦resolve or reject 状态必须发生改变
 */
class MyPromise {
  constructor(executor) {
    this.state = 'pending'
    this.value = ''
    this.reason = ''
    executor(resolve, reject)
    let resolve = (value) => {
      if (this.state === 'pending') {
        this.state = 'fullFilled'  //改变状态
        this.value = value   //存入形参的值
      }
    }
    let reject = (reason) => {
      if (this.state === 'pending') {
        this.state = 'rejected'
        this.reason = reason
      }
    }
  }
}


/**
 * resolve(10)  --->  then( res => console.log(10))
 * * 执行顺序,一旦resolve或者reject都会去执行then方法
 * * then方法是通过promise对象去调用的,所以需要定义一个then方法
 */

class MyPromise {
  constructor(executor) {
    this.state = 'pending'
    this.value = ''
    this.reason = ''
    executor(resolve, reject)
    let resolve = (value) => {
      if (this.state === 'pending') {
        this.state = 'fullFilled'  //改变状态
        this.value = value   //存入形参的值
      }
    }
    let reject = (reason) => {
      if (this.state === 'pending') {
        this.state = 'rejected'
        this.reason = reason
      }
    }
  }
  then() {

  }
}


/** new Promise().then(res=>{}, reason=>{})
 * * then方法接收两个参数,参数都是函数
 * * 一个是成功的回调,接收结果
 * * 一个是失败的回调,接收失败的错误
 */

 class MyPromise {
  constructor(executor) {
    this.state = 'pending'
    this.value = ''
    this.reason = ''
    executor(resolve, reject)
    let resolve = (value) => {
      if (this.state === 'pending') {
        this.state = 'fullFilled'  //改变状态
        this.value = value   //存入形参的值
      }
    }
    let reject = (reason) => {
      if (this.state === 'pending') {
        this.state = 'rejected'
        this.reason = reason
      }
    }
  }
  then(onFullFilled, onRejected) {
    if (this.state = 'fullFilled'){
      onFullFilled(this.value)
    }
    if (this.state = 'rejected'){
      onRejected(this.reason)
    }
  }
}


new MyPromise((resolve, reject) =>{
  resolve(10)
}).then(res => {
  console.log(res);
}, reason => {
  console.log(reason);
})

/**
 * new Promise 会捕获错误
 * * try catch
 */


 class MyPromise {
  constructor(executor) {
    this.state = 'pending'
    this.value = ''
    this.reason = ''
    let resolve = (value) => {
      if (this.state === 'pending') {
        this.state = 'fullFilled'  //改变状态
        this.value = value   //存入形参的值
      }
    }
    let reject = (reason) => {
      if (this.state === 'pending') {
        this.state = 'rejected'
        this.reason = reason
      }
    }
    try{
      executor(resolve, reject)
    }
    catch {
      reject(reason)
    }
  }
  then(onFullFilled, onRejected) {
    if (this.state = 'fullFilled'){
      onFullFilled(this.value)
    }
    if (this.state = 'rejected'){
      onRejected(this.reason)
    }
  }
}

/**
 * 发布订阅
 * 
 */

 class MyPromise {
  constructor(executor) {
    this.state = 'pending'
    this.value = ''
    this.reason = ''
    let resolve = (value) => {
      if (this.state === 'pending') {
        this.state = 'fullFilled'  //改变状态
        this.value = value   //存入形参的值
      }
    }
    let reject = (reason) => {
      if (this.state === 'pending') {
        this.state = 'rejected'
        this.reason = reason
      }
    }
    try{
      executor(resolve, reject)
    }
    catch {
      reject(reason)
    }
  }
  then(onFullFilled, onRejected) {
    if (this.state === 'fullFilled'){
      onFullFilled(this.value)
    }
    if (this.state === 'rejected'){
      onRejected(this.reason)
    }
  }
}